using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class ChatManager : Photon.MonoBehaviour {
	
	public bool isChatActive = false;
	
	public List<string> listChatLog = new List<string>();
	
	private string strChat = "Hello Admin...!!!";
	
	private Rect chat_RECT;
	
	private Vector2 scrollChatView = Vector2.zero;
	
	
	void Start () {
		chat_RECT = new Rect(0f,Screen.height*0.3f,Screen.width,Screen.height*0.6f);
	}
	
	void Update(){
		
	}
	
	
	void OnGUI(){
	
		if (isChatActive){
			
			GUILayout.BeginArea(chat_RECT);
			scrollChatView = GUILayout.BeginScrollView(scrollChatView);
			foreach(string temp in listChatLog){
				GUILayout.Label(temp);	
			}
			GUILayout.EndScrollView();
			
			
			GUI.SetNextControlName("ChatField");
			strChat = GUILayout.TextField(strChat,90);
			
			GUILayout.EndArea();

			if (Event.current.Equals(Event.KeyboardEvent("None")) && GUI.GetNameOfFocusedControl() == "ChatField"){
				if (strChat != ""){
					SendChatMessage(PhotonTargets.All,strChat);
				}
			}
		}
		
	}
	
	[RPC]
	void synSendChatMessage(string text, PhotonMessageInfo info){
		text = info.sender.name + " : " + text;
		listChatLog.Add(text);
		scrollChatView.y = 99999;	
	}
	
	void SendChatMessage(PhotonTargets target,string text){
		strChat = "";
		photonView.RPC("synSendChatMessage",target,text);	
	}	
}
